package com.xw.unification.config;

import com.xw.unification.condition.UnifiedExceptionCondition;
import com.xw.unification.handler.UnifiedExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.HandlerMethod;

/**
 * 统一异常处理组件自动配置类
 *
 * @author qiwei
 * @date 2023-8-9 21:00:00
 * @since v1.0.0
 */
@Configuration
@AutoConfigureAfter(WebUnificationMvcBaseConfig.class)
@EnableConfigurationProperties({WebUnificationMvcProperties.class})
@Conditional(UnifiedExceptionCondition.class)
public class WebUnificationMvcUnifiedExceptionConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebUnificationMvcUnifiedExceptionConfig.class);

    public WebUnificationMvcUnifiedExceptionConfig() {
        LOGGER.info("[WebUnificationMvcUnifiedExceptionConfig] ### UnifiedExceptionConfig auto configuration");
    }

    @RestControllerAdvice
    @Conditional(UnifiedExceptionCondition.class)
    public static class ExceptionHandlerMethodDelegate extends UnifiedExceptionHandler {

        @Autowired
        public ExceptionHandlerMethodDelegate(WebUnificationMvcProperties webUnificationMvcProperties) {
            super(webUnificationMvcProperties);
        }

        public static HandlerMethod getHandlerMethod() {
            return UnifiedExceptionHandler.getHandlerMethod();
        }
    }


}
